<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book python基础语言 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>元类 | python基础语言</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../01day/section.1.1.html" />
    
    
    <link rel="prev" href="../01day/index.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="1.1"
        data-chapter-title="元类"
        data-filepath="01day/section.1.0.md"
        data-basepath=".."
        data-revision="Fri Mar 17 2017 08:48:09 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="01day/index.html">
            
                
                    <a href="../01day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        面向对象进阶
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.1" data-path="01day/section.1.0.html">
            
                
                    <a href="../01day/section.1.0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        元类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01day/section.1.1.html">
            
                
                    <a href="../01day/section.1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        python是动态语言
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="01day/section.1.3.html">
            
                
                    <a href="../01day/section.1.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        __slots__
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="02day/section.4.html">
            
                
                    <a href="../02day/section.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        生成器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="02day/section.3.html">
            
                
                    <a href="../02day/section.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        迭代器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="02day/section.1.html">
            
                
                    <a href="../02day/section.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        闭包
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="02day/section.2.html">
            
                
                    <a href="../02day/section.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        装饰器
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="02day/index.html">
            
                
                    <a href="../02day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        其他的知识点
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="03day/section.0.2.html">
            
                
                    <a href="../03day/section.0.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        import导入模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="03day/section.1.1.html">
            
                
                    <a href="../03day/section.1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        循环导入
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="03day/section.0.3.html">
            
                
                    <a href="../03day/section.0.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        作用域
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="03day/section.2.html">
            
                
                    <a href="../03day/section.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        ==、is
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="03day/section.1.html">
            
                
                    <a href="../03day/section.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        深拷贝、浅拷贝
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="03day/section.6.html">
            
                
                    <a href="../03day/section.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        进制、位运算
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="01day/section.3.html">
            
                
                    <a href="../01day/section.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        私有化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="01day/section.3.1.html">
            
                
                    <a href="../01day/section.3.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                        属性property
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="03day/index.html">
            
                
                    <a href="../03day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        其他的知识点
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="03day/section.0.1.html">
            
                
                    <a href="../03day/section.0.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        垃圾回收(一)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="03day/section.0.1.1.html">
            
                
                    <a href="../03day/section.0.1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        垃圾回收(二)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="03day/section.0.1.2.html">
            
                
                    <a href="../03day/section.0.1.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        垃圾回收(三)-gc模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="01day/section.4.html">
            
                
                    <a href="../01day/section.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        内建属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="01day/section.6.html">
            
                
                    <a href="../01day/section.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        内建函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="01day/section.7.html">
            
                
                    <a href="../01day/section.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        集合set
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="02day/section.5.html">
            
                
                    <a href="../02day/section.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        functools
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="03day/section.4.html">
            
                
                    <a href="../03day/section.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                        模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="03day/section.3.html">
            
                
                    <a href="../03day/section.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                        调试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="03day/section.5.html">
            
                
                    <a href="../03day/section.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                        编码风格
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >python基础语言</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x5143;&#x7C7B;">&#x5143;&#x7C7B;</h1>
<h3 id="1-&#x7C7B;&#x4E5F;&#x662F;&#x5BF9;&#x8C61;">1. &#x7C7B;&#x4E5F;&#x662F;&#x5BF9;&#x8C61;</h3>
<p>&#x5728;&#x5927;&#x591A;&#x6570;&#x7F16;&#x7A0B;&#x8BED;&#x8A00;&#x4E2D;&#xFF0C;<span class="pg-emphasize pg-emphasize-yellow" style="background: #fff700;">&#x7C7B;&#x5C31;&#x662F;&#x4E00;&#x7EC4;&#x7528;&#x6765;&#x63CF;&#x8FF0;&#x5982;&#x4F55;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x4EE3;&#x7801;&#x6BB5;</span>&#x3002;&#x5728;Python&#x4E2D;&#x8FD9;&#x4E00;&#x70B9;&#x4ECD;&#x7136;&#x6210;&#x7ACB;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ObjectCreator</span><span class="hljs-params">(object)</span>:</span>
&#x2026;       <span class="hljs-keyword">pass</span>
&#x2026;
<span class="hljs-prompt">&gt;&gt;&gt; </span>my_object = ObjectCreator()
<span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-keyword">print</span> my_object
&lt;__main__.ObjectCreator object at <span class="hljs-number">0x8974f2c</span>&gt;
</code></pre>
<p>&#x4F46;&#x662F;&#xFF0C;Python&#x4E2D;&#x7684;&#x7C7B;&#x8FD8;&#x8FDC;&#x4E0D;&#x6B62;&#x5982;&#x6B64;&#x3002;<span class="pg-emphasize pg-emphasize-yellow" style="background: #fff700;">&#x7C7B;&#x540C;&#x6837;&#x4E5F;&#x662F;&#x4E00;&#x79CD;&#x5BF9;&#x8C61;</span>&#x3002;&#x662F;&#x7684;&#xFF0C;&#x6CA1;&#x9519;&#xFF0C;&#x5C31;&#x662F;&#x5BF9;&#x8C61;&#x3002;&#x53EA;&#x8981;&#x4F60;&#x4F7F;&#x7528;&#x5173;&#x952E;&#x5B57;class&#xFF0C;Python&#x89E3;&#x91CA;&#x5668;&#x5728;&#x6267;&#x884C;&#x7684;&#x65F6;&#x5019;&#x5C31;&#x4F1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x6BB5;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ObjectCreator</span><span class="hljs-params">(object)</span>:</span>
&#x2026;       <span class="hljs-keyword">pass</span>
&#x2026;
</code></pre>
<p>&#x5C06;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x540D;&#x5B57;&#x5C31;&#x662F;ObjectCreator&#x3002;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#xFF08;&#x7C7B;&#x5BF9;&#x8C61;ObjectCreator&#xFF09;&#x62E5;&#x6709;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#xFF08;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;&#xFF09;&#x7684;&#x80FD;&#x529B;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x5B83;&#x7684;&#x672C;&#x8D28;&#x4ECD;&#x7136;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x4E8E;&#x662F;&#x4E4E;&#x4F60;&#x53EF;&#x4EE5;&#x5BF9;&#x5B83;&#x505A;&#x5982;&#x4E0B;&#x7684;&#x64CD;&#x4F5C;&#xFF1A;</p>
<ol>
<li>&#x4F60;&#x53EF;&#x4EE5;&#x5C06;&#x5B83;&#x8D4B;&#x503C;&#x7ED9;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;</li>
<li>&#x4F60;&#x53EF;&#x4EE5;&#x62F7;&#x8D1D;&#x5B83;</li>
<li>&#x4F60;&#x53EF;&#x4EE5;&#x4E3A;&#x5B83;&#x589E;&#x52A0;&#x5C5E;&#x6027;</li>
<li>&#x4F60;&#x53EF;&#x4EE5;&#x5C06;&#x5B83;&#x4F5C;&#x4E3A;&#x51FD;&#x6570;&#x53C2;&#x6570;&#x8FDB;&#x884C;&#x4F20;&#x9012;</li>
</ol>
<p>&#x4E0B;&#x9762;&#x662F;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python">
&gt;&gt;&gt; print ObjectCreator     # &#x4F60;&#x53EF;&#x4EE5;&#x6253;&#x5370;&#x4E00;&#x4E2A;&#x7C7B;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x5176;&#x5B9E;&#x4E5F;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;
&lt;class &apos;__main__.ObjectCreator&apos;&gt;
&gt;&gt;&gt; def echo(o):
&#x2026;       print o
&#x2026;
&gt;&gt;&gt; echo(ObjectCreator)                 # &#x4F60;&#x53EF;&#x4EE5;&#x5C06;&#x7C7B;&#x505A;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x7ED9;&#x51FD;&#x6570;
&lt;class &apos;__main__.ObjectCreator&apos;&gt;
&gt;&gt;&gt; print hasattr(ObjectCreator, &apos;new_attribute&apos;)
Fasle
&gt;&gt;&gt; ObjectCreator.new_attribute = &apos;foo&apos; #  &#x4F60;&#x53EF;&#x4EE5;&#x4E3A;&#x7C7B;&#x589E;&#x52A0;&#x5C5E;&#x6027;
&gt;&gt;&gt; print hasattr(ObjectCreator, &apos;new_attribute&apos;)
True
&gt;&gt;&gt; print ObjectCreator.new_attribute
foo
&gt;&gt;&gt; ObjectCreatorMirror = ObjectCreator # &#x4F60;&#x53EF;&#x4EE5;&#x5C06;&#x7C7B;&#x8D4B;&#x503C;&#x7ED9;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;
&gt;&gt;&gt; print ObjectCreatorMirror()
&lt;__main__.ObjectCreator object at 0x8997b4c&gt;
</code></pre>
<h3 id="2-&#x52A8;&#x6001;&#x5730;&#x521B;&#x5EFA;&#x7C7B;">2. &#x52A8;&#x6001;&#x5730;&#x521B;&#x5EFA;&#x7C7B;</h3>
<p>&#x56E0;&#x4E3A;<span class="pg-emphasize pg-emphasize-yellow" style="background: #fff700;">&#x7C7B;&#x4E5F;&#x662F;&#x5BF9;&#x8C61;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x52A8;&#x6001;&#x7684;&#x521B;&#x5EFA;&#x5B83;&#x4EEC;&#xFF0C;&#x5C31;&#x50CF;&#x5176;&#x4ED6;&#x4EFB;&#x4F55;&#x5BF9;&#x8C61;&#x4E00;&#x6837;</span>&#x3002;&#x9996;&#x5148;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x51FD;&#x6570;&#x4E2D;&#x521B;&#x5EFA;&#x7C7B;&#xFF0C;&#x4F7F;&#x7528;class&#x5173;&#x952E;&#x5B57;&#x5373;&#x53EF;&#x3002;</p>
<pre><code class="lang-python">&gt;&gt;&gt; def choose_class(name):
&#x2026;       if name == &apos;foo&apos;:
&#x2026;           class Foo(object):
&#x2026;               pass
&#x2026;           return Foo     # &#x8FD4;&#x56DE;&#x7684;&#x662F;&#x7C7B;&#xFF0C;&#x4E0D;&#x662F;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;
&#x2026;       else:
&#x2026;           class Bar(object):
&#x2026;               pass
&#x2026;           return Bar
&#x2026;
&gt;&gt;&gt; MyClass = choose_class(&apos;foo&apos;)
&gt;&gt;&gt; print MyClass              # &#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x7C7B;&#xFF0C;&#x4E0D;&#x662F;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;
&lt;class &apos;__main__&apos;.Foo&gt;
&gt;&gt;&gt; print MyClass()            # &#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x8FD9;&#x4E2A;&#x7C7B;&#x521B;&#x5EFA;&#x7C7B;&#x5B9E;&#x4F8B;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x5BF9;&#x8C61;
&lt;__main__.Foo object at 0x89c6d4c&gt;
</code></pre>
<p>&#x4F46;&#x8FD9;&#x8FD8;&#x4E0D;&#x591F;&#x52A8;&#x6001;&#xFF0C;&#x56E0;&#x4E3A;&#x4F60;&#x4ECD;&#x7136;&#x9700;&#x8981;&#x81EA;&#x5DF1;&#x7F16;&#x5199;&#x6574;&#x4E2A;&#x7C7B;&#x7684;&#x4EE3;&#x7801;&#x3002;&#x7531;&#x4E8E;&#x7C7B;&#x4E5F;&#x662F;&#x5BF9;&#x8C61;&#xFF0C;&#x6240;&#x4EE5;&#x5B83;&#x4EEC;&#x5FC5;&#x987B;&#x662F;&#x901A;&#x8FC7;&#x4EC0;&#x4E48;&#x4E1C;&#x897F;&#x6765;&#x751F;&#x6210;&#x7684;&#x624D;&#x5BF9;&#x3002;<span class="pg-emphasize pg-emphasize-yellow" style="background: #fff700;">&#x5F53;&#x4F60;&#x4F7F;&#x7528;class&#x5173;&#x952E;&#x5B57;&#x65F6;&#xFF0C;Python&#x89E3;&#x91CA;&#x5668;&#x81EA;&#x52A8;&#x521B;&#x5EFA;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x3002;</span>&#x4F46;&#x5C31;&#x548C;Python&#x4E2D;&#x7684;&#x5927;&#x591A;&#x6570;&#x4E8B;&#x60C5;&#x4E00;&#x6837;&#xFF0C;Python&#x4ECD;&#x7136;&#x63D0;&#x4F9B;&#x7ED9;&#x4F60;&#x624B;&#x52A8;&#x5904;&#x7406;&#x7684;&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x8FD8;&#x8BB0;&#x5F97;&#x5185;&#x5EFA;&#x51FD;&#x6570;type&#x5417;&#xFF1F;&#x8FD9;&#x4E2A;&#x53E4;&#x8001;&#x4F46;&#x5F3A;&#x5927;&#x7684;&#x51FD;&#x6570;&#x80FD;&#x591F;&#x8BA9;&#x4F60;&#x77E5;&#x9053;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#x578B;&#x662F;&#x4EC0;&#x4E48;&#xFF0C;&#x5C31;&#x50CF;&#x8FD9;&#x6837;&#xFF1A;</p>
<pre><code class="lang-python">&gt;&gt;&gt; print type(1) #&#x6570;&#x503C;&#x7684;&#x7C7B;&#x578B;
&lt;type &apos;int&apos;&gt;
&gt;&gt;&gt; print type(&quot;1&quot;) #&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x7C7B;&#x578B;
&lt;type &apos;str&apos;&gt;
&gt;&gt;&gt; print type(ObjectCreator()) #&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#x578B;
&lt;class &apos;__main__.ObjectCreator&apos;&gt;
&gt;&gt;&gt; print type(ObjectCreator) #&#x7C7B;&#x7684;&#x7C7B;&#x578B;
&lt;type &apos;type&apos;&gt;
</code></pre>
<p>&#x4ED4;&#x7EC6;&#x89C2;&#x5BDF;&#x4E0A;&#x9762;&#x7684;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF0C;&#x53D1;&#x73B0;&#x4F7F;&#x7528;type&#x5BF9;ObjectCreator&#x67E5;&#x770B;&#x7C7B;&#x578B;&#x662F;&#xFF0C;&#x7B54;&#x6848;&#x4E3A;type&#xFF0C; &#x662F;&#x4E0D;&#x662F;&#x6709;&#x4E9B;&#x60CA;&#x8BB6;&#x3002;&#x3002;&#x3002;&#x770B;&#x4E0B;&#x9762;</p>
<h3 id="3-&#x4F7F;&#x7528;type&#x521B;&#x5EFA;&#x7C7B;">3. &#x4F7F;&#x7528;type&#x521B;&#x5EFA;&#x7C7B;</h3>
<p><span class="pg-emphasize pg-emphasize-yellow" style="background: #fff700;">type&#x8FD8;&#x6709;&#x4E00;&#x79CD;&#x5B8C;&#x5168;&#x4E0D;&#x540C;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x52A8;&#x6001;&#x7684;&#x521B;&#x5EFA;&#x7C7B;&#x3002;</span></p>
<p>type&#x53EF;&#x4EE5;&#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x7C7B;&#x7684;&#x63CF;&#x8FF0;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#xFF0C;&#x7136;&#x540E;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x7C7B;&#x3002;&#xFF08;&#x8981;&#x77E5;&#x9053;&#xFF0C;&#x6839;&#x636E;&#x4F20;&#x5165;&#x53C2;&#x6570;&#x7684;&#x4E0D;&#x540C;&#xFF0C;&#x540C;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x62E5;&#x6709;&#x4E24;&#x79CD;&#x5B8C;&#x5168;&#x4E0D;&#x540C;&#x7684;&#x7528;&#x6CD5;&#x662F;&#x4E00;&#x4EF6;&#x5F88;&#x50BB;&#x7684;&#x4E8B;&#x60C5;&#xFF0C;&#x4F46;&#x8FD9;&#x5728;Python&#x4E2D;&#x662F;&#x4E3A;&#x4E86;&#x4FDD;&#x6301;&#x5411;&#x540E;&#x517C;&#x5BB9;&#x6027;&#xFF09;</p>
<p>type&#x53EF;&#x4EE5;&#x50CF;&#x8FD9;&#x6837;&#x5DE5;&#x4F5C;&#xFF1A;</p>
<p><span class="pg-emphasize pg-emphasize-yellow" style="background: #fff700;">
type(&#x7C7B;&#x540D;, &#x7531;&#x7236;&#x7C7B;&#x540D;&#x79F0;&#x7EC4;&#x6210;&#x7684;&#x5143;&#x7EC4;&#xFF08;&#x9488;&#x5BF9;&#x7EE7;&#x627F;&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x53EF;&#x4EE5;&#x4E3A;&#x7A7A;&#xFF09;&#xFF0C;&#x5305;&#x542B;&#x5C5E;&#x6027;&#x7684;&#x5B57;&#x5178;&#xFF08;&#x540D;&#x79F0;&#x548C;&#x503C;&#xFF09;)
</span></p>
<p>&#x6BD4;&#x5982;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python">In [<span class="hljs-number">2</span>]: <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test</span>:</span> <span class="hljs-comment">#&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;Test&#x7C7B;</span>
   ...:     <span class="hljs-keyword">pass</span>
   ...:
In [<span class="hljs-number">3</span>]: Test() <span class="hljs-comment">#&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;Test&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;</span>
Out[<span class="hljs-number">3</span>]: &lt;__main__.Test at <span class="hljs-number">0x10d3f8438</span>&gt;
</code></pre>
<p>&#x53EF;&#x4EE5;&#x624B;&#x52A8;&#x50CF;&#x8FD9;&#x6837;&#x521B;&#x5EFA;&#xFF1A;</p>
<pre><code class="lang-python">Test2 = type(<span class="hljs-string">&quot;Test2&quot;</span>,(),{}) <span class="hljs-comment">#&#x5B9A;&#x4E86;&#x4E00;&#x4E2A;Test2&#x7C7B;</span>
In [<span class="hljs-number">5</span>]: Test2() <span class="hljs-comment">#&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;Test2&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;</span>
Out[<span class="hljs-number">5</span>]: &lt;__main__.Test2 at <span class="hljs-number">0x10d406b38</span>&gt;
</code></pre>
<p>&#x6211;&#x4EEC;&#x4F7F;&#x7528;&quot;Test2&quot;&#x4F5C;&#x4E3A;&#x7C7B;&#x540D;&#xFF0C;&#x5E76;&#x4E14;&#x4E5F;&#x53EF;&#x4EE5;&#x628A;&#x5B83;&#x5F53;&#x505A;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x6765;&#x4F5C;&#x4E3A;&#x7C7B;&#x7684;&#x5F15;&#x7528;&#x3002;&#x7C7B;&#x548C;&#x53D8;&#x91CF;&#x662F;&#x4E0D;&#x540C;&#x7684;&#xFF0C;&#x8FD9;&#x91CC;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x7406;&#x7531;&#x628A;&#x4E8B;&#x60C5;&#x5F04;&#x7684;&#x590D;&#x6742;&#x3002;&#x5373;type&#x51FD;&#x6570;&#x4E2D;&#x7B2C;1&#x4E2A;&#x5B9E;&#x53C2;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x53EB;&#x505A;&#x5176;&#x4ED6;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x8FD9;&#x4E2A;&#x540D;&#x5B57;&#x8868;&#x793A;&#x7C7B;&#x7684;&#x540D;&#x5B57;</p>
<pre><code class="lang-python">In [23]: MyDogClass = type(&apos;MyDog&apos;, (), {})

In [24]: print MyDogClass
&lt;class &apos;__main__.MyDog&apos;&gt;
</code></pre>
<p>&#x4F7F;&#x7528;help&#x6765;&#x6D4B;&#x8BD5;&#x8FD9;2&#x4E2A;&#x7C7B;</p>
<pre><code class="lang-python">
In [10]: help(Test) #&#x7528;help&#x67E5;&#x770B;Test&#x7C7B;

Help on class Test in module __main__:

class Test(builtins.object)
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables (if defined)
 |
 |  __weakref__
 |      list of weak references to the object (if defined)
</code></pre>
<pre><code class="lang-python">In [8]: help(Test2) #&#x7528;help&#x67E5;&#x770B;Test2&#x7C7B;

Help on class Test2 in module __main__:

class Test2(builtins.object)
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables (if defined)
 |
 |  __weakref__
 |      list of weak references to the object (if defined)
</code></pre>
<h3 id="4-&#x4F7F;&#x7528;type&#x521B;&#x5EFA;&#x5E26;&#x6709;&#x5C5E;&#x6027;&#x7684;&#x7C7B;">4. &#x4F7F;&#x7528;type&#x521B;&#x5EFA;&#x5E26;&#x6709;&#x5C5E;&#x6027;&#x7684;&#x7C7B;</h3>
<p>type &#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x5B57;&#x5178;&#x6765;&#x4E3A;&#x7C7B;&#x5B9A;&#x4E49;&#x5C5E;&#x6027;&#xFF0C;&#x56E0;&#x6B64;</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span>Foo = type(<span class="hljs-string">&apos;Foo&apos;</span>, (), {<span class="hljs-string">&apos;bar&apos;</span>:<span class="hljs-keyword">True</span>})
</code></pre>
<p>&#x53EF;&#x4EE5;&#x7FFB;&#x8BD1;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span><span class="hljs-params">(object)</span>:</span>
&#x2026;       bar = <span class="hljs-keyword">True</span>
</code></pre>
<p>&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x5C06;Foo&#x5F53;&#x6210;&#x4E00;&#x4E2A;&#x666E;&#x901A;&#x7684;&#x7C7B;&#x4E00;&#x6837;&#x4F7F;&#x7528;&#xFF1A;</p>
<pre><code class="lang-python">&gt;&gt;&gt; print Foo
&lt;class &apos;__main__.Foo&apos;&gt;
&gt;&gt;&gt; print Foo.bar
True
&gt;&gt;&gt; f = Foo()
&gt;&gt;&gt; print f
&lt;__main__.Foo object at 0x8a9b84c&gt;
&gt;&gt;&gt; print f.bar
True
</code></pre>
<p>&#x5F53;&#x7136;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5411;&#x8FD9;&#x4E2A;&#x7C7B;&#x7EE7;&#x627F;&#xFF0C;&#x6240;&#x4EE5;&#xFF0C;&#x5982;&#x4E0B;&#x7684;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FooChild</span><span class="hljs-params">(Foo)</span>:</span>
&#x2026;       <span class="hljs-keyword">pass</span>
</code></pre>
<p>&#x5C31;&#x53EF;&#x4EE5;&#x5199;&#x6210;&#xFF1A;</p>
<pre><code class="lang-python">&gt;&gt;&gt; FooChild = type(&apos;FooChild&apos;, (Foo,),{})
&gt;&gt;&gt; print FooChild
&lt;class &apos;__main__.FooChild&apos;&gt;
&gt;&gt;&gt; print FooChild.bar   # bar&#x5C5E;&#x6027;&#x662F;&#x7531;Foo&#x7EE7;&#x627F;&#x800C;&#x6765;
True
</code></pre>
<h4 id="&#x6CE8;&#x610F;&#xFF1A;">&#x6CE8;&#x610F;&#xFF1A;</h4>
<ul>
<li>type&#x7684;&#x7B2C;2&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x5143;&#x7EC4;&#x4E2D;&#x662F;&#x7236;&#x7C7B;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x5B57;&#x7B26;&#x4E32;</li>
<li>&#x6DFB;&#x52A0;&#x7684;&#x5C5E;&#x6027;&#x662F;&#x7C7B;&#x5C5E;&#x6027;&#xFF0C;&#x5E76;&#x4E0D;&#x662F;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;</li>
</ul>
<h3 id="5-&#x4F7F;&#x7528;type&#x521B;&#x5EFA;&#x5E26;&#x6709;&#x65B9;&#x6CD5;&#x7684;&#x7C7B;">5. &#x4F7F;&#x7528;type&#x521B;&#x5EFA;&#x5E26;&#x6709;&#x65B9;&#x6CD5;&#x7684;&#x7C7B;</h3>
<p>&#x6700;&#x7EC8;&#x4F60;&#x4F1A;&#x5E0C;&#x671B;&#x4E3A;&#x4F60;&#x7684;&#x7C7B;&#x589E;&#x52A0;&#x65B9;&#x6CD5;&#x3002;&#x53EA;&#x9700;&#x8981;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x6709;&#x7740;&#x6070;&#x5F53;&#x7B7E;&#x540D;&#x7684;&#x51FD;&#x6570;&#x5E76;&#x5C06;&#x5176;&#x4F5C;&#x4E3A;&#x5C5E;&#x6027;&#x8D4B;&#x503C;&#x5C31;&#x53EF;&#x4EE5;&#x4E86;&#x3002;</p>
<h5 id="&#x6DFB;&#x52A0;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;">&#x6DFB;&#x52A0;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;</h5>
<pre><code class="lang-python">In [<span class="hljs-number">46</span>]: <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">echo_bar</span><span class="hljs-params">(self)</span>:</span> <span class="hljs-comment">#&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x666E;&#x901A;&#x7684;&#x51FD;&#x6570;</span>
    ...:     print(self.bar)
    ...:

In [<span class="hljs-number">47</span>]: FooChild = type(<span class="hljs-string">&apos;FooChild&apos;</span>, (Foo,), {<span class="hljs-string">&apos;echo_bar&apos;</span>: echo_bar}) <span class="hljs-comment">#&#x8BA9;FooChild&#x7C7B;&#x4E2D;&#x7684;echo_bar&#x5C5E;&#x6027;&#xFF0C;&#x6307;&#x5411;&#x4E86;&#x4E0A;&#x9762;&#x5B9A;&#x4E49;&#x7684;&#x51FD;&#x6570;</span>

In [<span class="hljs-number">48</span>]: hasattr(Foo, <span class="hljs-string">&apos;echo_bar&apos;</span>) <span class="hljs-comment">#&#x5224;&#x65AD;Foo&#x7C7B;&#x4E2D;&#xFF0C;&#x662F;&#x5426;&#x6709;echo_bar&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;</span>
Out[<span class="hljs-number">48</span>]: <span class="hljs-keyword">False</span>

In [<span class="hljs-number">49</span>]:

In [<span class="hljs-number">49</span>]: hasattr(FooChild, <span class="hljs-string">&apos;echo_bar&apos;</span>) <span class="hljs-comment">#&#x5224;&#x65AD;FooChild&#x7C7B;&#x4E2D;&#xFF0C;&#x662F;&#x5426;&#x6709;echo_bar&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;</span>
Out[<span class="hljs-number">49</span>]: <span class="hljs-keyword">True</span>

In [<span class="hljs-number">50</span>]: my_foo = FooChild()

In [<span class="hljs-number">51</span>]: my_foo.echo_bar()
<span class="hljs-keyword">True</span>
</code></pre>
<h5 id="&#x6DFB;&#x52A0;&#x9759;&#x6001;&#x65B9;&#x6CD5;">&#x6DFB;&#x52A0;&#x9759;&#x6001;&#x65B9;&#x6CD5;</h5>
<pre><code class="lang-python">In [<span class="hljs-number">36</span>]: @staticmethod
    ...: <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">testStatic</span><span class="hljs-params">()</span>:</span>
    ...:     print(<span class="hljs-string">&quot;static method ....&quot;</span>)
    ...:

In [<span class="hljs-number">37</span>]: Foochild = type(<span class="hljs-string">&apos;Foochild&apos;</span>, (Foo,), {<span class="hljs-string">&quot;echo_bar&quot;</span>:echo_bar, <span class="hljs-string">&quot;testStatic&quot;</span>:
    ...: testStatic})

In [<span class="hljs-number">38</span>]: fooclid = Foochild()

In [<span class="hljs-number">39</span>]: fooclid.testStatic
Out[<span class="hljs-number">39</span>]: &lt;function __main__.testStatic&gt;

In [<span class="hljs-number">40</span>]: fooclid.testStatic()
static method ....

In [<span class="hljs-number">41</span>]: fooclid.echo_bar()
<span class="hljs-keyword">True</span>
</code></pre>
<h5 id="&#x6DFB;&#x52A0;&#x7C7B;&#x65B9;&#x6CD5;">&#x6DFB;&#x52A0;&#x7C7B;&#x65B9;&#x6CD5;</h5>
<pre><code class="lang-python">In [<span class="hljs-number">42</span>]: @classmethod
    ...: <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">testClass</span><span class="hljs-params">(cls)</span>:</span>
    ...:     print(cls.bar)
    ...:

In [<span class="hljs-number">43</span>]:

In [<span class="hljs-number">43</span>]: Foochild = type(<span class="hljs-string">&apos;Foochild&apos;</span>, (Foo,), {<span class="hljs-string">&quot;echo_bar&quot;</span>:echo_bar, <span class="hljs-string">&quot;testStatic&quot;</span>:
    ...: testStatic, <span class="hljs-string">&quot;testClass&quot;</span>:testClass})

In [<span class="hljs-number">44</span>]:

In [<span class="hljs-number">44</span>]: fooclid = Foochild()

In [<span class="hljs-number">45</span>]: fooclid.testClass()
<span class="hljs-keyword">True</span>
</code></pre>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;&#x5728;Python&#x4E2D;&#xFF0C;&#x7C7B;&#x4E5F;&#x662F;&#x5BF9;&#x8C61;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x52A8;&#x6001;&#x7684;&#x521B;&#x5EFA;&#x7C7B;&#x3002;&#x8FD9;&#x5C31;&#x662F;&#x5F53;&#x4F60;&#x4F7F;&#x7528;&#x5173;&#x952E;&#x5B57;class&#x65F6;Python&#x5728;&#x5E55;&#x540E;&#x505A;&#x7684;&#x4E8B;&#x60C5;&#xFF0C;&#x800C;&#x8FD9;&#x5C31;&#x662F;&#x901A;&#x8FC7;&#x5143;&#x7C7B;&#x6765;&#x5B9E;&#x73B0;&#x7684;&#x3002;</p>
<h3 id="6-&#x5230;&#x5E95;&#x4EC0;&#x4E48;&#x662F;&#x5143;&#x7C7B;&#xFF08;&#x7EC8;&#x4E8E;&#x5230;&#x4E3B;&#x9898;&#x4E86;&#xFF09;">6. &#x5230;&#x5E95;&#x4EC0;&#x4E48;&#x662F;&#x5143;&#x7C7B;&#xFF08;&#x7EC8;&#x4E8E;&#x5230;&#x4E3B;&#x9898;&#x4E86;&#xFF09;</h3>
<p><span class="pg-emphasize pg-emphasize-yellow" style="background: #fff700;">&#x5143;&#x7C7B;&#x5C31;&#x662F;&#x7528;&#x6765;&#x521B;&#x5EFA;&#x7C7B;&#x7684;&#x201C;&#x4E1C;&#x897F;&#x201D;&#x3002;</span>&#x4F60;&#x521B;&#x5EFA;&#x7C7B;&#x5C31;&#x662F;&#x4E3A;&#x4E86;&#x521B;&#x5EFA;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;&#xFF0C;&#x4E0D;&#x662F;&#x5417;&#xFF1F;&#x4F46;&#x662F;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x5B66;&#x4E60;&#x5230;&#x4E86;Python&#x4E2D;&#x7684;&#x7C7B;&#x4E5F;&#x662F;&#x5BF9;&#x8C61;&#x3002;</p>
<p>&#x5143;&#x7C7B;&#x5C31;&#x662F;&#x7528;&#x6765;&#x521B;&#x5EFA;&#x8FD9;&#x4E9B;&#x7C7B;&#xFF08;&#x5BF9;&#x8C61;&#xFF09;&#x7684;&#xFF0C;<span class="pg-emphasize pg-emphasize-yellow" style="background: #fff700;">&#x5143;&#x7C7B;&#x5C31;&#x662F;&#x7C7B;&#x7684;&#x7C7B;</span>&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x7406;&#x89E3;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-python">MyClass = MetaClass() <span class="hljs-comment">#&#x4F7F;&#x7528;&#x5143;&#x7C7B;&#x521B;&#x5EFA;&#x51FA;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x79F0;&#x4E3A;&#x201C;&#x7C7B;&#x201D;</span>
MyObject = MyClass() <span class="hljs-comment">#&#x4F7F;&#x7528;&#x201C;&#x7C7B;&#x201D;&#x6765;&#x521B;&#x5EFA;&#x51FA;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;</span>
</code></pre>
<p>&#x4F60;&#x5DF2;&#x7ECF;&#x770B;&#x5230;&#x4E86;type&#x53EF;&#x4EE5;&#x8BA9;&#x4F60;&#x50CF;&#x8FD9;&#x6837;&#x505A;&#xFF1A;</p>
<pre><code class="lang-python">MyClass = type(<span class="hljs-string">&apos;MyClass&apos;</span>, (), {})
</code></pre>
<p>&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x51FD;&#x6570;type&#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x4E00;&#x4E2A;&#x5143;&#x7C7B;&#x3002;type&#x5C31;&#x662F;Python&#x5728;&#x80CC;&#x540E;&#x7528;&#x6765;&#x521B;&#x5EFA;&#x6240;&#x6709;&#x7C7B;&#x7684;&#x5143;&#x7C7B;&#x3002;&#x73B0;&#x5728;&#x4F60;&#x60F3;&#x77E5;&#x9053;&#x90A3;&#x4E3A;&#x4EC0;&#x4E48;type&#x4F1A;&#x5168;&#x90E8;&#x91C7;&#x7528;&#x5C0F;&#x5199;&#x5F62;&#x5F0F;&#x800C;&#x4E0D;&#x662F;Type&#x5462;&#xFF1F;&#x597D;&#x5427;&#xFF0C;&#x6211;&#x731C;&#x8FD9;&#x662F;&#x4E3A;&#x4E86;&#x548C;str&#x4FDD;&#x6301;&#x4E00;&#x81F4;&#x6027;&#xFF0C;str&#x662F;&#x7528;&#x6765;&#x521B;&#x5EFA;&#x5B57;&#x7B26;&#x4E32;&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#xFF0C;&#x800C;int&#x662F;&#x7528;&#x6765;&#x521B;&#x5EFA;&#x6574;&#x6570;&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#x3002;type&#x5C31;&#x662F;&#x521B;&#x5EFA;&#x7C7B;&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x68C0;&#x67E5;__class__&#x5C5E;&#x6027;&#x6765;&#x770B;&#x5230;&#x8FD9;&#x4E00;&#x70B9;&#x3002;<span class="pg-emphasize pg-emphasize-yellow" style="background: #fff700;">Python&#x4E2D;&#x6240;&#x6709;&#x7684;&#x4E1C;&#x897F;&#xFF0C;&#x6CE8;&#x610F;&#xFF0C;&#x6211;&#x662F;&#x6307;&#x6240;&#x6709;&#x7684;&#x4E1C;&#x897F;&#x2014;&#x2014;&#x90FD;&#x662F;&#x5BF9;&#x8C61;&#x3002;&#x8FD9;&#x5305;&#x62EC;&#x6574;&#x6570;&#x3001;&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x51FD;&#x6570;&#x4EE5;&#x53CA;&#x7C7B;</span>&#x3002;&#x5B83;&#x4EEC;&#x5168;&#x90E8;&#x90FD;&#x662F;&#x5BF9;&#x8C61;&#xFF0C;&#x800C;&#x4E14;&#x5B83;&#x4EEC;&#x90FD;&#x662F;&#x4ECE;&#x4E00;&#x4E2A;&#x7C7B;&#x521B;&#x5EFA;&#x800C;&#x6765;&#xFF0C;&#x8FD9;&#x4E2A;&#x7C7B;&#x5C31;&#x662F;type&#x3002;</p>
<pre><code class="lang-python">&gt;&gt;&gt; age = 35
&gt;&gt;&gt; age.__class__
&lt;type &apos;int&apos;&gt;
&gt;&gt;&gt; name = &apos;bob&apos;
&gt;&gt;&gt; name.__class__
&lt;type &apos;str&apos;&gt;
&gt;&gt;&gt; def foo(): pass
&gt;&gt;&gt;foo.__class__
&lt;type &apos;function&apos;&gt;
&gt;&gt;&gt; class Bar(object): pass
&gt;&gt;&gt; b = Bar()
&gt;&gt;&gt; b.__class__
&lt;class &apos;__main__.Bar&apos;&gt;
</code></pre>
<p>&#x73B0;&#x5728;&#xFF0C;&#x5BF9;&#x4E8E;&#x4EFB;&#x4F55;&#x4E00;&#x4E2A;__class__&#x7684;__class__&#x5C5E;&#x6027;&#x53C8;&#x662F;&#x4EC0;&#x4E48;&#x5462;&#xFF1F;</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span>a.__class__.__class__
&lt;type <span class="hljs-string">&apos;type&apos;</span>&gt;
<span class="hljs-prompt">&gt;&gt;&gt; </span>age.__class__.__class__
&lt;type <span class="hljs-string">&apos;type&apos;</span>&gt;
<span class="hljs-prompt">&gt;&gt;&gt; </span>foo.__class__.__class__
&lt;type <span class="hljs-string">&apos;type&apos;</span>&gt;
<span class="hljs-prompt">&gt;&gt;&gt; </span>b.__class__.__class__
&lt;type <span class="hljs-string">&apos;type&apos;</span>&gt;
</code></pre>
<p>&#x56E0;&#x6B64;&#xFF0C;&#x5143;&#x7C7B;&#x5C31;&#x662F;&#x521B;&#x5EFA;&#x7C7B;&#x8FD9;&#x79CD;&#x5BF9;&#x8C61;&#x7684;&#x4E1C;&#x897F;&#x3002;type&#x5C31;&#x662F;Python&#x7684;&#x5185;&#x5EFA;&#x5143;&#x7C7B;&#xFF0C;&#x5F53;&#x7136;&#x4E86;&#xFF0C;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x81EA;&#x5DF1;&#x7684;&#x5143;&#x7C7B;&#x3002;</p>
<h3 id="7-metaclass&#x5C5E;&#x6027;">7. __metaclass__&#x5C5E;&#x6027;</h3>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x7C7B;&#x7684;&#x65F6;&#x5019;&#x4E3A;&#x5176;&#x6DFB;&#x52A0;__metaclass__&#x5C5E;&#x6027;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span><span class="hljs-params">(object)</span>:</span>
    __metaclass__ = something&#x2026;
    ...&#x7701;&#x7565;...
</code></pre>
<p>&#x5982;&#x679C;&#x4F60;&#x8FD9;&#x4E48;&#x505A;&#x4E86;&#xFF0C;Python&#x5C31;&#x4F1A;&#x7528;&#x5143;&#x7C7B;&#x6765;&#x521B;&#x5EFA;&#x7C7B;Foo&#x3002;&#x5C0F;&#x5FC3;&#x70B9;&#xFF0C;&#x8FD9;&#x91CC;&#x9762;&#x6709;&#x4E9B;&#x6280;&#x5DE7;&#x3002;&#x4F60;&#x9996;&#x5148;&#x5199;&#x4E0B;class Foo(object)&#xFF0C;&#x4F46;&#x662F;&#x7C7B;Foo&#x8FD8;&#x6CA1;&#x6709;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x521B;&#x5EFA;&#x3002;Python&#x4F1A;&#x5728;&#x7C7B;&#x7684;&#x5B9A;&#x4E49;&#x4E2D;&#x5BFB;&#x627E;__metaclass__&#x5C5E;&#x6027;&#xFF0C;&#x5982;&#x679C;&#x627E;&#x5230;&#x4E86;&#xFF0C;Python&#x5C31;&#x4F1A;&#x7528;&#x5B83;&#x6765;&#x521B;&#x5EFA;&#x7C7B;Foo&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x627E;&#x5230;&#xFF0C;&#x5C31;&#x4F1A;&#x7528;&#x5185;&#x5EFA;&#x7684;type&#x6765;&#x521B;&#x5EFA;&#x8FD9;&#x4E2A;&#x7C7B;&#x3002;&#x628A;&#x4E0B;&#x9762;&#x8FD9;&#x6BB5;&#x8BDD;&#x53CD;&#x590D;&#x8BFB;&#x51E0;&#x6B21;&#x3002;&#x5F53;&#x4F60;&#x5199;&#x5982;&#x4E0B;&#x4EE3;&#x7801;&#x65F6; :</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span><span class="hljs-params">(Bar)</span>:</span>
    <span class="hljs-keyword">pass</span>
</code></pre>
<p>Python&#x505A;&#x4E86;&#x5982;&#x4E0B;&#x7684;&#x64CD;&#x4F5C;&#xFF1A;</p>
<ol>
<li>Foo&#x4E2D;&#x6709;__metaclass__&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x5417;&#xFF1F;&#x5982;&#x679C;&#x662F;&#xFF0C;Python&#x4F1A;&#x901A;&#x8FC7;__metaclass__&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x540D;&#x5B57;&#x4E3A;Foo&#x7684;&#x7C7B;(&#x5BF9;&#x8C61;)</li>
<li>&#x5982;&#x679C;Python&#x6CA1;&#x6709;&#x627E;&#x5230;__metaclass__&#xFF0C;&#x5B83;&#x4F1A;&#x7EE7;&#x7EED;&#x5728;Bar&#xFF08;&#x7236;&#x7C7B;&#xFF09;&#x4E2D;&#x5BFB;&#x627E;__metaclass__&#x5C5E;&#x6027;&#xFF0C;&#x5E76;&#x5C1D;&#x8BD5;&#x505A;&#x548C;&#x524D;&#x9762;&#x540C;&#x6837;&#x7684;&#x64CD;&#x4F5C;&#x3002;</li>
<li>&#x5982;&#x679C;Python&#x5728;&#x4EFB;&#x4F55;&#x7236;&#x7C7B;&#x4E2D;&#x90FD;&#x627E;&#x4E0D;&#x5230;__metaclass__&#xFF0C;&#x5B83;&#x5C31;&#x4F1A;&#x5728;&#x6A21;&#x5757;&#x5C42;&#x6B21;&#x4E2D;&#x53BB;&#x5BFB;&#x627E;__metaclass__&#xFF0C;&#x5E76;&#x5C1D;&#x8BD5;&#x505A;&#x540C;&#x6837;&#x7684;&#x64CD;&#x4F5C;&#x3002;</li>
<li>&#x5982;&#x679C;&#x8FD8;&#x662F;&#x627E;&#x4E0D;&#x5230;__metaclass__,Python&#x5C31;&#x4F1A;&#x7528;&#x5185;&#x7F6E;&#x7684;type&#x6765;&#x521B;&#x5EFA;&#x8FD9;&#x4E2A;&#x7C7B;&#x5BF9;&#x8C61;&#x3002;</li>
</ol>
<p>&#x73B0;&#x5728;&#x7684;&#x95EE;&#x9898;&#x5C31;&#x662F;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5728;__metaclass__&#x4E2D;&#x653E;&#x7F6E;&#x4E9B;&#x4EC0;&#x4E48;&#x4EE3;&#x7801;&#x5462;&#xFF1F;&#x7B54;&#x6848;&#x5C31;&#x662F;&#xFF1A;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7C7B;&#x7684;&#x4E1C;&#x897F;&#x3002;&#x90A3;&#x4E48;&#x4EC0;&#x4E48;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7C7B;&#x5462;&#xFF1F;type&#xFF0C;&#x6216;&#x8005;&#x4EFB;&#x4F55;&#x4F7F;&#x7528;&#x5230;type&#x6216;&#x8005;&#x5B50;&#x7C7B;&#x5316;type&#x7684;&#x4E1C;&#x4E1C;&#x90FD;&#x53EF;&#x4EE5;&#x3002;</p>
<h3 id="8-&#x81EA;&#x5B9A;&#x4E49;&#x5143;&#x7C7B;">8. &#x81EA;&#x5B9A;&#x4E49;&#x5143;&#x7C7B;</h3>
<p>&#x5143;&#x7C7B;&#x7684;&#x4E3B;&#x8981;&#x76EE;&#x7684;&#x5C31;&#x662F;&#x4E3A;&#x4E86;&#x5F53;&#x521B;&#x5EFA;&#x7C7B;&#x65F6;&#x80FD;&#x591F;&#x81EA;&#x52A8;&#x5730;&#x6539;&#x53D8;&#x7C7B;&#x3002;&#x901A;&#x5E38;&#xFF0C;&#x4F60;&#x4F1A;&#x4E3A;API&#x505A;&#x8FD9;&#x6837;&#x7684;&#x4E8B;&#x60C5;&#xFF0C;&#x4F60;&#x5E0C;&#x671B;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x7B26;&#x5408;&#x5F53;&#x524D;&#x4E0A;&#x4E0B;&#x6587;&#x7684;&#x7C7B;&#x3002;</p>
<p>&#x5047;&#x60F3;&#x4E00;&#x4E2A;&#x5F88;&#x50BB;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x4F60;&#x51B3;&#x5B9A;&#x5728;&#x4F60;&#x7684;&#x6A21;&#x5757;&#x91CC;&#x6240;&#x6709;&#x7684;&#x7C7B;&#x7684;&#x5C5E;&#x6027;&#x90FD;&#x5E94;&#x8BE5;&#x662F;&#x5927;&#x5199;&#x5F62;&#x5F0F;&#x3002;&#x6709;&#x597D;&#x51E0;&#x79CD;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x529E;&#x5230;&#xFF0C;&#x4F46;&#x5176;&#x4E2D;&#x4E00;&#x79CD;&#x5C31;&#x662F;&#x901A;&#x8FC7;&#x5728;&#x6A21;&#x5757;&#x7EA7;&#x522B;&#x8BBE;&#x5B9A;__metaclass__&#x3002;&#x91C7;&#x7528;&#x8FD9;&#x79CD;&#x65B9;&#x6CD5;&#xFF0C;&#x8FD9;&#x4E2A;&#x6A21;&#x5757;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x7C7B;&#x90FD;&#x4F1A;&#x901A;&#x8FC7;&#x8FD9;&#x4E2A;&#x5143;&#x7C7B;&#x6765;&#x521B;&#x5EFA;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x544A;&#x8BC9;&#x5143;&#x7C7B;&#x628A;&#x6240;&#x6709;&#x7684;&#x5C5E;&#x6027;&#x90FD;&#x6539;&#x6210;&#x5927;&#x5199;&#x5F62;&#x5F0F;&#x5C31;&#x4E07;&#x4E8B;&#x5927;&#x5409;&#x4E86;&#x3002;</p>
<p>&#x5E78;&#x8FD0;&#x7684;&#x662F;&#xFF0C;__metaclass__&#x5B9E;&#x9645;&#x4E0A;&#x53EF;&#x4EE5;&#x88AB;&#x4EFB;&#x610F;&#x8C03;&#x7528;&#xFF0C;&#x5B83;&#x5E76;&#x4E0D;&#x9700;&#x8981;&#x662F;&#x4E00;&#x4E2A;&#x6B63;&#x5F0F;&#x7684;&#x7C7B;&#x3002;&#x6240;&#x4EE5;&#xFF0C;&#x6211;&#x4EEC;&#x8FD9;&#x91CC;&#x5C31;&#x5148;&#x4EE5;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x4F8B;&#x5B50;&#x5F00;&#x59CB;&#x3002;</p>
<h5 id="python2&#x4E2D;">python2&#x4E2D;</h5>
<pre><code class="lang-python"><span class="hljs-comment">#-*- coding:utf-8 -*-</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">upper_attr</span><span class="hljs-params">(future_class_name, future_class_parents, future_class_attr)</span>:</span>

    <span class="hljs-comment">#&#x904D;&#x5386;&#x5C5E;&#x6027;&#x5B57;&#x5178;&#xFF0C;&#x628A;&#x4E0D;&#x662F;__&#x5F00;&#x5934;&#x7684;&#x5C5E;&#x6027;&#x540D;&#x5B57;&#x53D8;&#x4E3A;&#x5927;&#x5199;</span>
    newAttr = {}
    <span class="hljs-keyword">for</span> name,value <span class="hljs-keyword">in</span> future_class_attr.items():
        <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> name.startswith(<span class="hljs-string">&quot;__&quot;</span>):
            newAttr[name.upper()] = value

    <span class="hljs-comment">#&#x8C03;&#x7528;type&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7C7B;</span>
    <span class="hljs-keyword">return</span> type(future_class_name, future_class_parents, newAttr)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span><span class="hljs-params">(object)</span>:</span>
    __metaclass__ = upper_attr <span class="hljs-comment">#&#x8BBE;&#x7F6E;Foo&#x7C7B;&#x7684;&#x5143;&#x7C7B;&#x4E3A;upper_attr</span>
    bar = <span class="hljs-string">&apos;bip&apos;</span>

print(hasattr(Foo, <span class="hljs-string">&apos;bar&apos;</span>))
print(hasattr(Foo, <span class="hljs-string">&apos;BAR&apos;</span>))

f = Foo()
print(f.BAR)
</code></pre>
<h5 id="python3&#x4E2D;">python3&#x4E2D;</h5>
<pre><code class="lang-python"><span class="hljs-comment">#-*- coding:utf-8 -*-</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">upper_attr</span><span class="hljs-params">(future_class_name, future_class_parents, future_class_attr)</span>:</span>

    <span class="hljs-comment">#&#x904D;&#x5386;&#x5C5E;&#x6027;&#x5B57;&#x5178;&#xFF0C;&#x628A;&#x4E0D;&#x662F;__&#x5F00;&#x5934;&#x7684;&#x5C5E;&#x6027;&#x540D;&#x5B57;&#x53D8;&#x4E3A;&#x5927;&#x5199;</span>
    newAttr = {}
    <span class="hljs-keyword">for</span> name,value <span class="hljs-keyword">in</span> future_class_attr.items():
        <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> name.startswith(<span class="hljs-string">&quot;__&quot;</span>):
            newAttr[name.upper()] = value

    <span class="hljs-comment">#&#x8C03;&#x7528;type&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7C7B;</span>
    <span class="hljs-keyword">return</span> type(future_class_name, future_class_parents, newAttr)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span><span class="hljs-params">(object, metaclass=upper_attr)</span>:</span>
    bar = <span class="hljs-string">&apos;bip&apos;</span>

print(hasattr(Foo, <span class="hljs-string">&apos;bar&apos;</span>))
print(hasattr(Foo, <span class="hljs-string">&apos;BAR&apos;</span>))

f = Foo()
print(f.BAR)
</code></pre>
<p>&#x73B0;&#x5728;&#x8BA9;&#x6211;&#x4EEC;&#x518D;&#x505A;&#x4E00;&#x6B21;&#xFF0C;&#x8FD9;&#x4E00;&#x6B21;&#x7528;&#x4E00;&#x4E2A;&#x771F;&#x6B63;&#x7684;class&#x6765;&#x5F53;&#x505A;&#x5143;&#x7C7B;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-comment">#coding=utf-8</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UpperAttrMetaClass</span><span class="hljs-params">(type)</span>:</span>
    <span class="hljs-comment"># __new__ &#x662F;&#x5728;__init__&#x4E4B;&#x524D;&#x88AB;&#x8C03;&#x7528;&#x7684;&#x7279;&#x6B8A;&#x65B9;&#x6CD5;</span>
    <span class="hljs-comment"># __new__&#x662F;&#x7528;&#x6765;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x5E76;&#x8FD4;&#x56DE;&#x4E4B;&#x7684;&#x65B9;&#x6CD5;</span>
    <span class="hljs-comment"># &#x800C;__init__&#x53EA;&#x662F;&#x7528;&#x6765;&#x5C06;&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;&#x521D;&#x59CB;&#x5316;&#x7ED9;&#x5BF9;&#x8C61;</span>
    <span class="hljs-comment"># &#x4F60;&#x5F88;&#x5C11;&#x7528;&#x5230;__new__&#xFF0C;&#x9664;&#x975E;&#x4F60;&#x5E0C;&#x671B;&#x80FD;&#x591F;&#x63A7;&#x5236;&#x5BF9;&#x8C61;&#x7684;&#x521B;&#x5EFA;</span>
    <span class="hljs-comment"># &#x8FD9;&#x91CC;&#xFF0C;&#x521B;&#x5EFA;&#x7684;&#x5BF9;&#x8C61;&#x662F;&#x7C7B;&#xFF0C;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x80FD;&#x591F;&#x81EA;&#x5B9A;&#x4E49;&#x5B83;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x8FD9;&#x91CC;&#x6539;&#x5199;__new__</span>
    <span class="hljs-comment"># &#x5982;&#x679C;&#x4F60;&#x5E0C;&#x671B;&#x7684;&#x8BDD;&#xFF0C;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x5728;__init__&#x4E2D;&#x505A;&#x4E9B;&#x4E8B;&#x60C5;</span>
    <span class="hljs-comment"># &#x8FD8;&#x6709;&#x4E00;&#x4E9B;&#x9AD8;&#x7EA7;&#x7684;&#x7528;&#x6CD5;&#x4F1A;&#x6D89;&#x53CA;&#x5230;&#x6539;&#x5199;__call__&#x7279;&#x6B8A;&#x65B9;&#x6CD5;&#xFF0C;&#x4F46;&#x662F;&#x6211;&#x4EEC;&#x8FD9;&#x91CC;&#x4E0D;&#x7528;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__new__</span><span class="hljs-params">(cls, future_class_name, future_class_parents, future_class_attr)</span>:</span>
        <span class="hljs-comment">#&#x904D;&#x5386;&#x5C5E;&#x6027;&#x5B57;&#x5178;&#xFF0C;&#x628A;&#x4E0D;&#x662F;__&#x5F00;&#x5934;&#x7684;&#x5C5E;&#x6027;&#x540D;&#x5B57;&#x53D8;&#x4E3A;&#x5927;&#x5199;</span>
        newAttr = {}
        <span class="hljs-keyword">for</span> name,value <span class="hljs-keyword">in</span> future_class_attr.items():
            <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> name.startswith(<span class="hljs-string">&quot;__&quot;</span>):
                newAttr[name.upper()] = value

        <span class="hljs-comment"># &#x65B9;&#x6CD5;1&#xFF1A;&#x901A;&#x8FC7;&apos;type&apos;&#x6765;&#x505A;&#x7C7B;&#x5BF9;&#x8C61;&#x7684;&#x521B;&#x5EFA;</span>
        <span class="hljs-comment"># return type(future_class_name, future_class_parents, newAttr)</span>

        <span class="hljs-comment"># &#x65B9;&#x6CD5;2&#xFF1A;&#x590D;&#x7528;type.__new__&#x65B9;&#x6CD5;</span>
        <span class="hljs-comment"># &#x8FD9;&#x5C31;&#x662F;&#x57FA;&#x672C;&#x7684;OOP&#x7F16;&#x7A0B;&#xFF0C;&#x6CA1;&#x4EC0;&#x4E48;&#x9B54;&#x6CD5;</span>
        <span class="hljs-comment"># return type.__new__(cls, future_class_name, future_class_parents, newAttr)</span>

        <span class="hljs-comment"># &#x65B9;&#x6CD5;3&#xFF1A;&#x4F7F;&#x7528;super&#x65B9;&#x6CD5;</span>
        <span class="hljs-keyword">return</span> super(UpperAttrMetaClass, cls).__new__(cls, future_class_name, future_class_parents, newAttr)

<span class="hljs-comment">#python2&#x7684;&#x7528;&#x6CD5;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span><span class="hljs-params">(object)</span>:</span>
    __metaclass__ = UpperAttrMetaClass
    bar = <span class="hljs-string">&apos;bip&apos;</span>

<span class="hljs-comment"># python3&#x7684;&#x7528;&#x6CD5;</span>
<span class="hljs-comment"># class Foo(object, metaclass = UpperAttrMetaClass):</span>
<span class="hljs-comment">#     bar = &apos;bip&apos;</span>

print(hasattr(Foo, <span class="hljs-string">&apos;bar&apos;</span>))
<span class="hljs-comment"># &#x8F93;&#x51FA;: False</span>
print(hasattr(Foo, <span class="hljs-string">&apos;BAR&apos;</span>))
<span class="hljs-comment"># &#x8F93;&#x51FA;:True</span>

f = Foo()
print(f.BAR)
<span class="hljs-comment"># &#x8F93;&#x51FA;:&apos;bip&apos;</span>
</code></pre>
<p>&#x5C31;&#x662F;&#x8FD9;&#x6837;&#xFF0C;&#x9664;&#x6B64;&#x4E4B;&#x5916;&#xFF0C;&#x5173;&#x4E8E;&#x5143;&#x7C7B;&#x771F;&#x7684;&#x6CA1;&#x6709;&#x522B;&#x7684;&#x53EF;&#x8BF4;&#x7684;&#x4E86;&#x3002;&#x4F46;&#x5C31;&#x5143;&#x7C7B;&#x672C;&#x8EAB;&#x800C;&#x8A00;&#xFF0C;&#x5B83;&#x4EEC;&#x5176;&#x5B9E;&#x662F;&#x5F88;&#x7B80;&#x5355;&#x7684;&#xFF1A;</p>
<ol>
<li>&#x62E6;&#x622A;&#x7C7B;&#x7684;&#x521B;&#x5EFA;</li>
<li>&#x4FEE;&#x6539;&#x7C7B;</li>
<li>&#x8FD4;&#x56DE;&#x4FEE;&#x6539;&#x4E4B;&#x540E;&#x7684;&#x7C7B;</li>
</ol>
<h5 id="&#x7A76;&#x7ADF;&#x4E3A;&#x4EC0;&#x4E48;&#x8981;&#x4F7F;&#x7528;&#x5143;&#x7C7B;&#xFF1F;">&#x7A76;&#x7ADF;&#x4E3A;&#x4EC0;&#x4E48;&#x8981;&#x4F7F;&#x7528;&#x5143;&#x7C7B;&#xFF1F;</h5>
<p>&#x73B0;&#x5728;&#x56DE;&#x5230;&#x6211;&#x4EEC;&#x7684;&#x5927;&#x4E3B;&#x9898;&#x4E0A;&#x6765;&#xFF0C;&#x7A76;&#x7ADF;&#x662F;&#x4E3A;&#x4EC0;&#x4E48;&#x4F60;&#x4F1A;&#x53BB;&#x4F7F;&#x7528;&#x8FD9;&#x6837;&#x4E00;&#x79CD;&#x5BB9;&#x6613;&#x51FA;&#x9519;&#x4E14;&#x6666;&#x6DA9;&#x7684;&#x7279;&#x6027;&#xFF1F;&#x597D;&#x5427;&#xFF0C;&#x4E00;&#x822C;&#x6765;&#x8BF4;&#xFF0C;&#x4F60;&#x6839;&#x672C;&#x5C31;&#x7528;&#x4E0D;&#x4E0A;&#x5B83;&#xFF1A;</p>
<p>&#x201C;&#x5143;&#x7C7B;&#x5C31;&#x662F;&#x6DF1;&#x5EA6;&#x7684;&#x9B54;&#x6CD5;&#xFF0C;<span class="pg-emphasize pg-emphasize-yellow" style="background: #fff700;">99%&#x7684;&#x7528;&#x6237;&#x5E94;&#x8BE5;&#x6839;&#x672C;&#x4E0D;&#x5FC5;&#x4E3A;&#x6B64;&#x64CD;&#x5FC3;</span>&#x3002;&#x5982;&#x679C;&#x4F60;&#x60F3;&#x641E;&#x6E05;&#x695A;&#x7A76;&#x7ADF;&#x662F;&#x5426;&#x9700;&#x8981;&#x7528;&#x5230;&#x5143;&#x7C7B;&#xFF0C;&#x90A3;&#x4E48;&#x4F60;&#x5C31;&#x4E0D;&#x9700;&#x8981;&#x5B83;&#x3002;&#x90A3;&#x4E9B;&#x5B9E;&#x9645;&#x7528;&#x5230;&#x5143;&#x7C7B;&#x7684;&#x4EBA;&#x90FD;&#x975E;&#x5E38;&#x6E05;&#x695A;&#x5730;&#x77E5;&#x9053;&#x4ED6;&#x4EEC;&#x9700;&#x8981;&#x505A;&#x4EC0;&#x4E48;&#xFF0C;&#x800C;&#x4E14;&#x6839;&#x672C;&#x4E0D;&#x9700;&#x8981;&#x89E3;&#x91CA;&#x4E3A;&#x4EC0;&#x4E48;&#x8981;&#x7528;&#x5143;&#x7C7B;&#x3002;&#x201D;  &#x2014;&#x2014; Python&#x754C;&#x7684;&#x9886;&#x8896; Tim Peters</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../01day/index.html" class="navigation navigation-prev " aria-label="Previous page: 面向对象进阶"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../01day/section.1.1.html" class="navigation navigation-next " aria-label="Next page: python是动态语言"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book python基础语言 -->
</html>
